﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<script type="text/javascript" src="bob.js"></script>
<script type="text/javascript">

    //bob.js can define javascript code namespaces - similar to C#.
    bob.ns.setNs('bobjs.sample', {

        SampleCode: function (nArrayLength) {

            var self = this;

            var minLength = 1;
            var maxLength = 10;

            //bob.js provides validation functions.
            if (!bob.utils.isNumber(nArrayLength)) {
                throw new Error('Wrong argument: nArrayLength.');
            }
            else if (!((nArrayLength) >= minLength && (nArrayLength <= maxLength))) {
                //bob.js allows string formatting - similar to C#.
                var errorText = bob.string.formatString('Argument nArrayLength must be between {0} and {1}.', minLength, maxLength);
                throw new Error(errorText);
            }

            //bob.js introduces read-only property (using functions).
            bob.prop.namedProp(this, 'arrayLength', nArrayLength, true);

            var getRandom = function () {
                return Math.round(Math.random() * 100);
            };

            this.constructArray = function () {
                var resArray = [];
                for (var i = 0; i < self.get_arrayLength(); i++) {
                    resArray.push(getRandom());
                }
                return resArray;
            };

            this.addValue = function (nValue, arr) {
                //validate arguments using bob.js.
                if (!bob.utils.isNumber(nValue)) {
                    throw new Error('Wrong argument: nValue');
                }
                if (!bob.utils.isArray(arr)) {
                    throw new Error('Wrong argument: arr.');
                }
                else if (arr.length != self.get_arrayLength()) {
                    var errorText = bob.string.formatString('Array arr length ({0}) must equal to {1}.', arr.length, self.get_arrayLength());
                    throw new Error(errorText);
                }

                //bob.js collection extensions allow enumerations.
                bob.collections.extensions.forEach(arr, function (index, item) {
                    arr[index] = item + nValue;
                });
                return arr;
            };
        },

        runSample: function (nArrayLength, nValue) {

            document.write('========================<br />');

            document.write(
                bob.string.formatString('runSample({0}, {1}).', nArrayLength, nValue)
                );

            var code = null;
            try {
                code = new bobjs.sample.SampleCode(nArrayLength);
            }
            catch (error) {
                document.write(
                    bob.string.formatString('Error thrown when executing new SampleCode({0}): {1}.', nArrayLength, error.message)
                    );
                document.write('<br />');
                return false;
            }

            var array = null;
            try {
                array = code.constructArray();
                document.write('Array constructed when calling constructArray(): ');

                //bob.js can convert any object to string representation.
                document.write(
                    bob.serialization.objectToString(array)
                    );
                document.write('<br />');
            }
            catch (error) {
                document.write(
                    bob.string.formatString('Error thrown when executing new constructArray(): {0}.', error.message)
                    );
                document.write('<br />');
                return false;
            }

            try {
                code.addValue(nValue, array);
                document.write(
                    bob.string.formatString('Array result after calling addValue({0}, [array]): ', nValue)
                    );
                document.write(bob.serialization.objectToString(array));
                document.write('<br />');
            }
            catch (error) {
                document.write(
                    bob.string.formatString('Error thrown when executing new addValue({0}, [array]): {1}.', nValue, error.message)
                    );
                document.write('<br />');
                return false;
            }

            document.write('<br />');
            return true;
        }
    });

</script>
</head>
<body>

Running sample code:<br />

<script type="text/javascript">

    bobjs.sample.runSample(0, 0);
    bobjs.sample.runSample(5, 0);
    bobjs.sample.runSample(5, 1);

</script>

</body>
</html>
